<template>
	<view class="content">
		<!-- 订单信息 -->
		<view class="goods-info">
			<h1>订单信息</h1>
			<!-- 订单详情 -->
			<view class="meal">
				<div class="item" v-if="type !== 'yuepai'">
					<span>
						<image :src="imageUrl + 'sucai.png'" mode=""></image>
						<text>已选素材：</text>
					</span>
					<text>{{ getNumber(orderInfo.order.mediaIds) }} 份</text>
				</div>
				<div class="item">
					<span>
						<image :src="imageUrl + 'sucai.png'" mode=""></image>
						<text>套餐：</text>
					</span>
					<text>{{ type == 'yuepai' ? orderInfo.orderDto.description :orderInfo.order.setMealName }}</text>
				</div>
				<div class="item">
					<span>
						<image :src="imageUrl + 'sucai.png'" mode=""></image>
						<text>实付金额：</text>
					</span>
					<text>{{type == 'yuepai' ? (orderInfo.skiDeposit.orderTotal>=0 ? orderInfo.skiDeposit.orderTotal:0) : (orderInfo.total>=0 ? orderInfo.total : 0) }} 元</text>
				</div>
				<div class="item">
					<span>
						<image :src="imageUrl + 'sucai.png' " mode=""></image>
						<text>订单编号：</text>
					</span>
					<text>{{ orderInfo.weChatPayDto.out_trade_no }}</text>
				</div>
				<div class="item">
					<span>
						<image :src="imageUrl + 'sucai.png'" mode=""></image>
						<text>{{ type == 'yuepai' ? '约定时间：' : '创建时间：' }} </text>
					</span>
					<text>{{  type == 'yuepai' ? orderInfo.orderDto.reservationTime : orderInfo.order.orderTime }}</text>
				</div>
			</view>

		</view>

		<!-- 确认支付 -->
		<view class="paymentBtn">
			<button @click="fukuanBtn">确认支付</button>
		</view>
	</view>
</template>

<script>
	import { submitEditingJob } from '@/api/service.js'
	import {
		payMentInfo,
		payMentData,
		
	} from "@/api/payment.js"
	import {
		getMealData,
		getUserData,
		removeCartList,
		removeMealData
	} from '@/utils/storage.js'
	import imageConfig from '@/utils/images.js'
	export default {
		data() {
			return {
				imageUrl:imageConfig.uploadImageUrl,
				// 订单信息
				orderInfo: {},
				setMealName: getMealData('meal_data').setMealName,
				mediaIds:[],
				type:''
			}
		},
		onLoad(options) {
			this.orderInfo = JSON.parse(decodeURIComponent(options.orderInfo))
			console.log(options,'00')
			console.log(this.orderInfo,'this.orderInfo')
			// this.mediaIds = options.mediaIds.split(",")
			if(options.mediaIds){
				this.mediaIds = options.mediaIds
			}else{
				this.type = 'yuepai'
			}
			
		},
		computed: {

		},
		methods: {
			getNumber(obj){
				if(obj){
					const lengthList = obj.split(",")
					console.log(lengthList)
					return lengthList.length
				}
			},
			// 支付
			fukuanBtn() {
				if(this.type !== 'yuepai'){
					let data = {
						out_trade_no: this.orderInfo.weChatPayDto.out_trade_no, //订单编号
						description: this.orderInfo.weChatPayDto.description, //订单描述
						amount: {
							currency:this.orderInfo.weChatPayDto.amount.currency,
							total:this.orderInfo.weChatPayDto.amount.total >= 0 ? this.orderInfo.weChatPayDto.amount.total : 0.01, // 在这直接判断一个小于0的值直接归零
						}, //	商品信息类
						payer: this.orderInfo.weChatPayDto.payer
					}
					// 调用后台接口，获取生成商户订单参数
					payMentData(data).then(resp => {
						removeCartList()
						removeMealData()
						console.log('resp----------: ', resp)
						// 获取支付信息成功后，使用官方 API 调起微信支付
						uni.requestPayment({
							timeStamp: resp.data.timeStamp,
							nonceStr: resp.data.nonceStr,
							package: resp.data.package,
							// signType: resp.data.signType,
							paySign: resp.data.paySign,
							signType: 'MD5',
							success(res) {
								console.log('success------: ', res)
								console.log('支付成功');
								uni.switchTab({
									url:"/pages/order/index"
								})
							},
							fail(err) {
								console.log('err------: ', err)
								console.log('支付失败');
							},
						})
					})
				}else{
					let data = {
						out_trade_no: this.orderInfo.weChatPayDto.out_trade_no, //订单编号
						description: this.orderInfo.weChatPayDto.description, //订单描述
						amount: this.orderInfo.weChatPayDto.amount, //	商品信息类
						payer: this.orderInfo.weChatPayDto.payer
					}
					// 调用后台接口，获取生成商户订单参数
					payMentData(data).then(resp => {
						removeCartList()
						removeMealData()
						console.log('resp----------: ', resp)
						// 获取支付信息成功后，使用官方 API 调起微信支付
						uni.requestPayment({
							timeStamp: resp.data.timeStamp,
							nonceStr: resp.data.nonceStr,
							package: resp.data.package,
							// signType: resp.data.signType,
							paySign: resp.data.paySign,
							signType: 'MD5',
							success(res) {
								console.log('success------: ', res)
								console.log('支付成功');
								uni.switchTab({
									url:"/pages/order/index"
								})
							},
							fail(err) {
								console.log('err------: ', err)
								console.log('支付失败');
							},
						})
					})
				}
				
				
			}
		}
	}
</script>

<style lang="less">
	.content {

		.goods-info {
			margin: 0 auto;
			margin-top: 36rpx;
			width: 714rpx;
			padding: 30rpx 0;
			background: #FFFFFF;
			box-shadow: 0rpx 2rpx 5rpx 0rpx rgba(0, 0, 0, 0.19);
			border-radius: 36rpx;

			h1 {
				margin-left: 18rpx;
				font-size: 33rpx;
				color: #0A0A0A;
				padding-bottom: 20rpx;
				border-bottom: 1px solid #F2F2F2;
			}


			.meal {

				margin: 20rpx 20rpx 0 20rpx;

				.item {
					display: flex;
					justify-content: space-between;
					padding: 8rpx 0;

					>span {
						display: flex;
						align-items: center;

						image {
							margin-right: 8rpx;
							width: 28rpx;
							height: 28rpx;
						}
					}

					text {
						font-size: 24rpx;
						color: #3D3D3D;
					}
				}

			}

		}

		.paymentBtn {
			margin-top: 36rpx;

			button {
				width: 362rpx;
				background: #4CA2EC;
				border-radius: 36rpx;
				font-size: 33rpx;
				color: #FFFFFF;
			}
		}
	}
</style>